package com.jlastu.androiduidemo.views;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.jlastu.androiduidemo.R;
import com.jlastu.androiduidemo.bean.Course;
import com.jlastu.androiduidemo.bean.Student;
import com.jlastu.androiduidemo.database.DBHelper;
import com.jlastu.androiduidemo.widget.HeaderView;

public class AddCourseActivity extends AppCompatActivity implements View.OnClickListener{
    private HeaderView mHeaderView;
    private EditText mEtCname;
    private EditText mEtCode;
    private EditText mEtTeacher;
    private EditText mEtHours;
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_course);
        mHeaderView = findViewById(R.id.title_bar);
        mHeaderView.setTitle("添加课程");
        mEtCname = findViewById(R.id.et_course_name);
        mEtCode = findViewById(R.id.et_course_code);
        mEtTeacher = findViewById(R.id.et_course_teacher);
        mEtHours = findViewById(R.id.et_learning_hours);
        mHeaderView.setRightClickListener(this);
    }
    public void onClick(View v) {//右上角的添加图标的点击事件，点击开始添加

        String course_code = mEtCode.getText().toString();
        String course_name = mEtCname.getText().toString();
        String course_teacher = mEtTeacher.getText().toString();
        String learning_hours = mEtHours.getText().toString();

        //判断输入内容不能是空
        if (allInputNotEmpty(course_name, course_code,course_teacher, learning_hours)) {
            Course course = new Course();
           course.setCourse_code(course_code);
           course.setCourse_name(course_name);
           course.setCourse_teacher(course_teacher);
           course.setLearning_hours(learning_hours);
            //将输入的数据插入数据库中
            int id = (int) DBHelper.getInstance(this).insertCourse2(course);
            if (id >= 0) {
                Toast.makeText(this, "添加成功,可继续添加", Toast.LENGTH_SHORT).show();

                mEtCode.setText("");
                mEtCname.setText("");
                mEtTeacher.setText("");
                mEtHours.setText("");
            }

        }
    }
    private boolean allInputNotEmpty(String... inputStrings) {
        for (int i = 0; i < inputStrings.length; i++) {
            if (TextUtils.isEmpty(inputStrings[i])) {
                return false;
            }
        }
        return true;
    }
}
